local duoshou = fk.CreateSkill {

  name = "joy__duoshou",

  tags = { Skill.Compulsory, },

}



duoshou:addEffect(fk.Damage, {
  name = "joy__duoshou",
  anim_type = "drawcard",
  events = {fk.Damage,fk.TargetSpecified},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.Damage then
      return target == player and player:hasSkill(duoshou.name) and
    table.contains(player:getTableMark("@joy_duoshou-turn"), "sanshou_damage") and data.to and not data.to:isNude() 
    elseif event == fk.TargetSpecified then
      if
      player:hasSkill(duoshou.name) and
      target == player and
      data.card.trueName == "slash" and
      data.firstTarget and player:getMark(duoshou.name.."-turn") == 0
    then
      local room = player.room
      local logic = room.logic
        local n = #logic:getEventsOfScope(GameEvent.UseCard,2, function (e)
          local use = e.data
          if use.card.trueName == "slash" and use.from == target.id then
            return true
          end
          return false
        end, Player.HistoryTurn)
      return n == 1
    end
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.Damage then
      local mark = player:getTableMark("@joy_duoshou-turn")
      table.removeOne(mark, "sanshou_damage")
      player.room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      if data.to ~= data.from then
        local card = player.room:askForCardsChosen(player,data.to,1,1,"he",duoshou.name,"选择 %src 一张牌获得:"..data.to.id)
        if #card > 0 then
          player.room:obtainCard(player,card,false,fk.ReasonPrey,player.id,duoshou.name)
        end
      end
    elseif event == fk.TargetSpecified then
      player.room:setPlayerMark(player, duoshou.name.."-turn",1)
      player:drawCards(2, duoshou.name)
    end
  end,
})
duoshou:addEffect(fk.TargetSpecified, {
  name = "joy__duoshou",
  anim_type = "drawcard",
  events = {fk.Damage,fk.TargetSpecified},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.Damage then
      return target == player and player:hasSkill(duoshou.name) and
    table.contains(player:getTableMark("@joy_duoshou-turn"), "sanshou_damage") and data.to and not data.to:isNude() 
    elseif event == fk.TargetSpecified then
      if
      player:hasSkill(duoshou.name) and
      target == player and
      data.card.trueName == "slash" and
      data.firstTarget and player:getMark(duoshou.name.."-turn") == 0
    then
      local room = player.room
      local logic = room.logic
        local n = #logic:getEventsOfScope(GameEvent.UseCard,2, function (e)
          local use = e.data
          if use.card.trueName == "slash" and use.from == target.id then
            return true
          end
          return false
        end, Player.HistoryTurn)
      return n == 1
    end
      return false
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.Damage then
      local mark = player:getTableMark("@joy_duoshou-turn")
      table.removeOne(mark, "sanshou_damage")
      player.room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      if data.to ~= data.from then
        local card = player.room:askForCardsChosen(player,data.to,1,1,"he",duoshou.name,"选择 %src 一张牌获得:"..data.to.id)
        if #card > 0 then
          player.room:obtainCard(player,card,false,fk.ReasonPrey,player.id,duoshou.name)
        end
      end
    elseif event == fk.TargetSpecified then
      player.room:setPlayerMark(player, duoshou.name.."-turn",1)
      player:drawCards(2, duoshou.name)
    end
  end,
})
duoshou:addEffect(fk.PreCardUse, {
  refresh_events = {fk.PreCardUse, fk.TurnStart, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return player == target and data == self
    else
      return player:hasSkill(self, true) and (event == fk.TurnStart or player == target)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardUse then
      local mark = player:getTableMark("@joy_duoshou-turn")
      local update = false
      if data.card.color == Card.Red and table.removeOne(mark, "sanshou_red") then
        update = true
      end
      if data.card.type == Card.TypeBasic and table.removeOne(mark, "sanshou_basic") then
        update = true
        if player:hasSkill(duoshou.name) then
          data.extraUse = true
        end
      end
      if update then
        room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      end
    elseif event == fk.TurnStart then
      room:setPlayerMark(player, "@joy_duoshou-turn", {"sanshou_red", "sanshou_basic", "sanshou_damage"})
    elseif event == fk.EventAcquireSkill then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local a, b, c = true, true, true
      local use = nil
      player.room.logic:getEventsByRule( GameEvent.UseCard, 1, function (e)
        use = e.data
        if use.from == player.id then
          if use.card.color == Card.Red then
            a = false
          end
          if use.card.type == Card.TypeBasic then
            b = false
          end
          return not (a or b)
        end
      end, turn_event.id)
      local mark = {}
      if a then
        table.insert(mark, "sanshou_red")
      end
      if b then
        table.insert(mark, "sanshou_basic")
      end
      room.logic:getActualDamageEvents(1, function (e)
        use = e.data
        if use.from == player then
          c = false
          return true
        end
        return false
      end, nil, turn_event.id)
      if c then
        table.insert(mark, "sanshou_damage")
      end
      room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@joy_duoshou-turn", 0)
    end
  end,
})
duoshou:addEffect(fk.TurnStart, {
  refresh_events = {fk.PreCardUse, fk.TurnStart, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return player == target and data == self
    else
      return player:hasSkill(self, true) and (event == fk.TurnStart or player == target)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardUse then
      local mark = player:getTableMark("@joy_duoshou-turn")
      local update = false
      if data.card.color == Card.Red and table.removeOne(mark, "sanshou_red") then
        update = true
      end
      if data.card.type == Card.TypeBasic and table.removeOne(mark, "sanshou_basic") then
        update = true
        if player:hasSkill(duoshou.name) then
          data.extraUse = true
        end
      end
      if update then
        room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      end
    elseif event == fk.TurnStart then
      room:setPlayerMark(player, "@joy_duoshou-turn", {"sanshou_red", "sanshou_basic", "sanshou_damage"})
    elseif event == fk.EventAcquireSkill then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local a, b, c = true, true, true
      local use = nil
      player.room.logic:getEventsByRule( GameEvent.UseCard, 1, function (e)
        use = e.data
        if use.from == player.id then
          if use.card.color == Card.Red then
            a = false
          end
          if use.card.type == Card.TypeBasic then
            b = false
          end
          return not (a or b)
        end
      end, turn_event.id)
      local mark = {}
      if a then
        table.insert(mark, "sanshou_red")
      end
      if b then
        table.insert(mark, "sanshou_basic")
      end
      room.logic:getActualDamageEvents(1, function (e)
        use = e.data
        if use.from == player then
          c = false
          return true
        end
        return false
      end, nil, turn_event.id)
      if c then
        table.insert(mark, "sanshou_damage")
      end
      room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@joy_duoshou-turn", 0)
    end
  end,
})
duoshou:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.PreCardUse, fk.TurnStart, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return player == target and data == self
    else
      return player:hasSkill(self, true) and (event == fk.TurnStart or player == target)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardUse then
      local mark = player:getTableMark("@joy_duoshou-turn")
      local update = false
      if data.card.color == Card.Red and table.removeOne(mark, "sanshou_red") then
        update = true
      end
      if data.card.type == Card.TypeBasic and table.removeOne(mark, "sanshou_basic") then
        update = true
        if player:hasSkill(duoshou.name) then
          data.extraUse = true
        end
      end
      if update then
        room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      end
    elseif event == fk.TurnStart then
      room:setPlayerMark(player, "@joy_duoshou-turn", {"sanshou_red", "sanshou_basic", "sanshou_damage"})
    elseif event == fk.EventAcquireSkill then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local a, b, c = true, true, true
      local use = nil
      player.room.logic:getEventsByRule( GameEvent.UseCard, 1, function (e)
        use = e.data
        if use.from == player.id then
          if use.card.color == Card.Red then
            a = false
          end
          if use.card.type == Card.TypeBasic then
            b = false
          end
          return not (a or b)
        end
      end, turn_event.id)
      local mark = {}
      if a then
        table.insert(mark, "sanshou_red")
      end
      if b then
        table.insert(mark, "sanshou_basic")
      end
      room.logic:getActualDamageEvents(1, function (e)
        use = e.data
        if use.from == player then
          c = false
          return true
        end
        return false
      end, nil, turn_event.id)
      if c then
        table.insert(mark, "sanshou_damage")
      end
      room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@joy_duoshou-turn", 0)
    end
  end,
})
duoshou:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.PreCardUse, fk.TurnStart, fk.EventAcquireSkill, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return player == target and data == self
    else
      return player:hasSkill(self, true) and (event == fk.TurnStart or player == target)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardUse then
      local mark = player:getTableMark("@joy_duoshou-turn")
      local update = false
      if data.card.color == Card.Red and table.removeOne(mark, "sanshou_red") then
        update = true
      end
      if data.card.type == Card.TypeBasic and table.removeOne(mark, "sanshou_basic") then
        update = true
        if player:hasSkill(duoshou.name) then
          data.extraUse = true
        end
      end
      if update then
        room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
      end
    elseif event == fk.TurnStart then
      room:setPlayerMark(player, "@joy_duoshou-turn", {"sanshou_red", "sanshou_basic", "sanshou_damage"})
    elseif event == fk.EventAcquireSkill then
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      local a, b, c = true, true, true
      local use = nil
      player.room.logic:getEventsByRule( GameEvent.UseCard, 1, function (e)
        use = e.data
        if use.from == player.id then
          if use.card.color == Card.Red then
            a = false
          end
          if use.card.type == Card.TypeBasic then
            b = false
          end
          return not (a or b)
        end
      end, turn_event.id)
      local mark = {}
      if a then
        table.insert(mark, "sanshou_red")
      end
      if b then
        table.insert(mark, "sanshou_basic")
      end
      room.logic:getActualDamageEvents(1, function (e)
        use = e.data
        if use.from == player then
          c = false
          return true
        end
        return false
      end, nil, turn_event.id)
      if c then
        table.insert(mark, "sanshou_damage")
      end
      room:setPlayerMark(player, "@joy_duoshou-turn", #mark > 0 and mark or 0)
    elseif event == fk.EventLoseSkill then
      room:setPlayerMark(player, "@joy_duoshou-turn", 0)
    end
  end,
})

duoshou:addEffect("targetmod", {
  name = "#joy_duoshou_targetmod",
  bypass_distances =  function(self, player, skill, card, to)
    return player:hasSkill(duoshou.name) and card and card.color == Card.Red and
    table.contains(player:getTableMark("@joy_duoshou-turn"), "sanshou_red")
  end,
})

return duoshou